var cm=multiCheckModel_create();

var gridColumns=[new Ext.grid.RowNumberer(), cm,{
	header:'系统编号',
	align : 'center',
	dataIndex: "messageId",
	id : 'messageId',
	menuDisabled:true,
	hidden :true
},{
    header: "标题",
    align : 'center',
    dataIndex: "name",
    id : 'name',
    width: 200
}, {
    header: "发布人",
    align : 'center',
    dataIndex: "author",
    id : 'author',
    width: 100
}, {
    header: "消息类型",
    align : 'center',
    dataIndex: "msgTypeName",
    id : 'msgTypeName',
    width: 100
}, {
    header: "发布时间",
    align : 'center',
    dataIndex: "utime",
    id : 'utime',
    width: 150
}, {
    header: "编辑",
    align : 'center',
	width: 60,
	dataIndex: "",
    resizable: false,
	hideable :false,
    renderer:function(){
		return '<span style="cursor:pointer;color:blue;">修改</span>'
	}
}]

function getFieldsFromColumns(colums){
	var fs=new Array();
	Ext.each(colums,function(col){
		fs.push(col.dataIndex);
	})
	return fs;
}

var gridStore=new Ext.data.JsonStore({
	 proxy: new Ext.data.HttpProxy({
       url : 'queryMessage.action',
        failure: function(){
            Ext.MessageBox.alert("错误", "数据源获取失败!");
        }
    }),
	totalProperty: 'obj.totalSize', 
	successProperty :'success',
	root:'obj.returnBolist',
	messageProperty:'msg',
	fields : getFieldsFromColumns(gridColumns)
});


var gridModel=new Ext.grid.ColumnModel({
  defaultSortable: true,
  defaultWidth: 85,
  columns:gridColumns
})

var gridConfig = {
	store : gridStore,
	cm : gridModel,
//	fbar : gridFootBt,
	sm : cm,
	animCollapse : true,
	frame : true,
	bbar : new Ext.PagingToolbar({
		pageSize : 15,
		store : gridStore,
		displayInfo : true,
		displyMsg : '显示第{0}条到第{1}条记录，一共{2}条',
		emptyMsg : "没有记录",
		items : [{
			text : '删除',
			style :'float:left',
			handler : function(bt, e) {
				var grid = bt.ownerCt.ownerCt;
				var cm = grid.getSelectionModel();
				var rds = cm.getSelections();
				Ext.Msg.confirm("提示", '你确信要执行删除操作？', function(at, mg) {
					if (at == 'yes') {
						var messageStr = null;
						Ext.each(rds, function(rd) {
							messageStr = (messageStr != null) ? (messageStr + ";" + rd.get('messageId')) : rd.get('messageId');
						})
						var con = new connection({
							url : 'deleteMessage.action',
							params : {
								json : messageStr
							},
							success : function(result, response) {
								searchMessageInfo();
								var alt = result.success ? '成功' : '失败';
								Ext.Msg.alert(alt, result.msg);
							},
							failure : function(result, response) {
								searchMessageInfo();
								var alt = result.success ? '成功' : '失败';
								Ext.Msg.alert(alt, result.msg);
							}
						});
						con.submit();
					}
				});
			}
		}]
	}),
	listeners : {
		render : function initGrid(grid) {

		},
		cellclick : function cellClick(grid, row, col, e) {
			var cm = grid.getColumnModel();
			var header = cm.getColumnHeader(col);
			if (header == '编辑') {
				var record = grid.getStore().getAt(row);

				var win = new Ext.Window(updateMessageWindowConfig);
				var form = new Ext.form.FormPanel(messageFormConfig)
				win.add(form);
				win.show();
				initFormData(form, record);
			}
		}
	}
}

/** 初始化该Grid; */
function initFormData(form, rd){
    var con = new connection({
        url: 'findMessageByMessageId.action',
        params: {
            json: rd.get("messageId")
        },
        success: function(rep, opt){
            if (rep.success) {
              // panel_initDataId(form,null,rep.obj);
			  Ext.getCmp('messageId').setValue(rep.obj.messageId);
			  Ext.getCmp('name').setValue(rep.obj.name);
			  Ext.getCmp('msgType_fl').setValue(rep.obj.msgType);
			  Ext.getCmp('content').setValue(rep.obj.content);
            }
            else {
                Ext.Msg.alert('错误 ', "消息(" + rd.get('name') + ")加载失败!");
            }
        }
    })
    con.submit();
}

function showAddMessageWindow(){
	var win=new Ext.Window(addMessageWindowConfig);
	var form=new Ext.form.FormPanel(messageFormConfig);
	win.add(form);
	win.show();
}
var topbar = [{
    icon: 'back/extJs/images/system/messages.png',
    xtype: 'button',
    text: "发布消息",
    handler: showAddMessageWindow
}];
var messageGrid = new Ext.grid.GridPanel(gridConfig);
	
var detailPanel=new Ext.FormPanel({
	layout:'column',
	frame:true,
	id:'searchMessagePanel',
	regetion:'north',
	defaults:{
		columnWidth:0.16,
		layout:'form',
		labelWidth:60,
		defaults:{
			xtype:'textfield',
			anchor: '90%'
		}
	},
	items:[{
		labelWidth:40,
		items:[{
			fieldLabel:'标&nbsp;&nbsp;&nbsp;题',
			name : 'msgTitle',
			id:'msgTitle'
		}]
	},{
		items:[{
			fieldLabel:'&nbsp;消息类型',
			name:'msgType',
			xtype:'combo',
			editable : false,
			mode:"local",
			value:'0',
			triggerAction : 'all',
			displayField : 'text',
			valueField : 'value',
			store:msgTypeStore
		}]
	},{	
		items:[{
			xtype:'datefield',
			fieldLabel:'&nbsp;发布时间',
			anchor: '93%',
			format : 'Y-m-d',
			name : 'dateFrom',
			id:'dateFrom'
		}]
	},{	
		labelWidth:15,
		columnWidth:0.12,
		items:[{
			xtype:'datefield',
			labelSeparator:'-',
			fieldLabel:'-',
			format : 'Y-m-d',
			name : 'dateTo',
			id:'dateTo'
		}]
	},{	
		items:[{
			fieldLabel:'&nbsp;发布人',
			id:'author',
			name : 'author'
		}]
	},{
		items:[{
			xtype:'button',
			text:'查询',
			id:'searchBt',
			anchor: '30%',
			handler:function(){
				searchMessageInfo(panel_gatherData(detailPanel));
			}
		}]
	}]
})
    Ext.onReady(function initFrame(){
        new Ext.Viewport({
			xtype: 'panel',
			layout: 'fit',
			items: [{
				xtype: 'panel',
				tbar: topbar,
				layout: 'border',
				items: [{
					region: 'north',
					split: true,
					border: false,
					height: 40,
					collapseMode: 'mini',
					layout: 'fit',
					items: [detailPanel]
				}, {
					region: 'center',
					split: true,
					border: false,
					layout: 'fit',
					items: [messageGrid]
				}]
			}]
        });
    })
	
  /**查询消息; */
function searchMessageInfo(data) {
	var store = messageGrid.getStore();
	if (!Ext.isEmpty(data)) {
		Ext.apply(store.baseParams, {
			json: Ext.encode(data)
		})
	}
	store.load({});
}